<template>
	<view>

		<view class="time">
			<text>还剩</text>
			<view class="shijian">{{countdownh}}</view>
			<text>时</text>
			<view class="shijian">{{countdownm}}</view>
			<text>分</text>
			<view class="shijian">{{countdowns}}</view>
			<text>秒</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				countdownh: '',
				countdownm: '',
				countdowns: '',
				timer: null, //重复执行
			}
		},
		onLoad() {
			this.timer = setInterval(() => {
				this.showtime()
			})

		},
		methods: {
			showtime() {
				var nowtime = new Date(), //获取当前时间
					endtime = new Date("2023/06/12 16:56:00"); //定义结束时间
				var lefttime = endtime.getTime() - nowtime.getTime(), //距离结束时间的毫秒数
					leftd = Math.floor(lefttime / (1000 * 60 * 60 * 24)), //计算天数
					lefth = Math.floor((lefttime / (1000 * 60 * 60) % 24) + leftd * 24) < 10 ? "0" + Math.floor((lefttime /
						(1000 * 60 * 60) % 24) + leftd * 24) : Math.floor((lefttime / (1000 * 60 * 60) % 24) + leftd *
					24), //计算小时数
					leftm = Math.floor(lefttime / (1000 * 60) % 60) < 10 ? "0" + Math.floor(lefttime / (1000 * 60) % 60) :
					Math.floor(lefttime / (1000 * 60) % 60), //计算分钟数
					lefts = Math.floor(lefttime / 1000 % 60) < 10 ? "0" + Math.floor(lefttime / 1000 % 60) : Math.floor(
						lefttime / 1000 % 60); //计算秒数
				this.countdownh = lefth //返回倒计时的字符串
				this.countdownm = leftm //返回倒计时的字符串
				this.countdowns = lefts //返回倒计时的字符串
				// 倒计时结束时，显示00:00:00
				if (lefttime < 0) {
					this.countdownh = this.countdownm = this.countdowns = "00"
					uni.showToast({
						title: '结束倒计时',
						icon: 'none'
					})
					clearInterval(this.timer)
				}
			}

		}
	}
</script>

<style>

</style>